package com.machine.filling.Test.ad;

import android.content.Context;
import android.media.MediaPlayer;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.signature.StringSignature;
import com.machine.filling.R;
import com.machine.filling.net.http.response.ADListResBean;
import com.machine.filling.util.SpUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/3/12 0012.
 */
@Deprecated
public class AdPagerAdapter extends PagerAdapter {
    private Context mContext;
    private List<ADListResBean.AdListBean> listBean;
    private static final String TAG = "AdPagerAdapter";
    private MediaPlayer mPlayer;
    private SurfaceView surfaceview;


    public AdPagerAdapter(Context mContext, List<ADListResBean.AdListBean> listBean) {
        this.mContext = mContext;
        if (listBean == null || listBean.size() == 0) {
            this.listBean = new ArrayList<>();
        } else {
            this.listBean = listBean;
        }
        mPlayer = new MediaPlayer();
        mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mPlayer.start();
                Log.e(TAG, "onPrepared() returned: " + "准备好了");
            }
        });
        Log.e(TAG, "mediaplayer: adapterCreate" + mPlayer);
    }


    public Object instantiateItem(ViewGroup container, final int position) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_ad, null);
        ImageView imageView = view.findViewById(R.id.iv_ad);
        final SurfaceView surfaceview = view.findViewById(R.id.surfaceview);

        if (listBean.get(position).getType() == 1) {//图片
            Log.e(TAG, "mediaplayer: 显示图片 mplayer处理之前" + mPlayer);
            imageView.setVisibility(View.VISIBLE);
            surfaceview.setVisibility(View.GONE);
            if (mPlayer != null) {
                if (mPlayer.isPlaying()) {
                    mPlayer.stop();
                }
                mPlayer.release();
                mPlayer = null;
            }
            Log.e(TAG, "mediaplayer: 显示图片 player处理之后" + mPlayer);
            Glide.with(mContext).load(listBean.get(position).getImgPath())
                    .signature(new StringSignature(String.valueOf(
                            SpUtils.getInstance(mContext).getAdListChangeTime())))
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .centerCrop()
                    .into(imageView);
        } else {
            imageView.setVisibility(View.GONE);
            surfaceview.setVisibility(View.VISIBLE);
            Log.e(TAG, "mediaplayer: 视频显示,处理之前" + mPlayer);
            if (mPlayer == null) {
                mPlayer = new MediaPlayer();
                mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        mPlayer.start();
                        Log.e(TAG, "onPrepared() returned: " + "准备好了");
                    }
                });
                mPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                    @Override
                    public boolean onError(MediaPlayer mp, int what, int extra) {
                        return true;
                    }
                });
            }

            Log.e(TAG, "mediaplayer: 视频显示,处理之后" + mPlayer);
            //设置需要播放的视频
            try {
                mPlayer.setDataSource(listBean.get(position).getImgPath());
                //把视频画面输出到SurfaceView

                mPlayer.prepareAsync();

            } catch (IOException e) {
                e.printStackTrace();
            }

            surfaceview.getHolder().addCallback(new SurfaceHolder.Callback() {
                @Override
                public void surfaceCreated(SurfaceHolder holder) {
                    mPlayer.setDisplay(holder);
                    mPlayer.start();
                    //播放
                    Log.e(TAG, "surfaceCreated() returned: " + "创建");
                }

                @Override
                public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                    Log.e(TAG, "surfaceChanged() returned: " + "改变");
                }

                @Override
                public void surfaceDestroyed(SurfaceHolder holder) {
                    Log.e(TAG, "surfaceDestroyed() returned: " + "销毁");

                }
            });
        }

        container.addView(view);
        return view;


/*
        if (listBean.get(position).getType() == 1 && position != 0) {//图片
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_ad_img, null);
            ImageView imageView = view.findViewById(R.id.iv_ad);
            Glide.with(mContext).load(listBean.get(position).getImgPath())
                    .signature(new StringSignature(String.valueOf(
                            SpUtils.getInstance(mContext).getAdListChangeTime())))
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .centerCrop()
                    .into(imageView);
            container.addView(imageView);
            return imageView;
        } else {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_ad_media, null);
            final SurfaceView surfaceview = view.findViewById(R.id.surfaceview);
            try {
                mPlayer.setDataSource(listBean.get(position).getImgPath());
            } catch (IOException e) {
                e.printStackTrace();
            }
            surfaceview.getHolder().setFormat(PixelFormat.TRANSPARENT);
            surfaceview.getHolder().addCallback(new SurfaceHolder.Callback() {
                @Override
                public void surfaceCreated(SurfaceHolder holder) {
                    mPlayer.setDisplay(holder);
                    mPlayer.prepareAsync();
                    Log.e(TAG, "surfaceCreated() returned: " + "创建");
                }

                @Override
                public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                    Log.e(TAG, "surfaceChanged() returned: " + "改变");
                }

                @Override
                public void surfaceDestroyed(SurfaceHolder holder) {
                    Log.e(TAG, "surfaceDestroyed() returned: " + "销毁");
                    mPlayer.reset();

                }
            });

            container.addView(surfaceview);
            return surfaceview;
        }*/


/*        if (listBean.get(position).getType() == 1 && position != 0) {//图片
            imageView = new ImageView(mContext);
            Glide.with(mContext).load(listBean.get(position).getImgPath())
                    .signature(new StringSignature(String.valueOf(SpUtils.getInstance(mContext).getAdListChangeTime())))
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .centerCrop()
                    .into(imageView);
            container.addView(imageView);

            return imageView;
        } else {//视频
            mPlayer1 = new MediaPlayer();
            try {
                mPlayer1.setDataSource(listBean.get(position).getImgPath());
                mPlayer1.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        mp.setOnInfoListener(new MediaPlayer.OnInfoListener() {
                            @Override
                            public boolean onInfo(MediaPlayer mp, int what, int extra) {
                                if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
                                    // video started
                                    surfaceview.setBackgroundColor(Color.TRANSPARENT);
                                    return true;
                                }
                                return false;
                            }
                        });
                        mPlayer1.start();

                        Log.e(TAG, "onPrepared() returned: " + "准备好了");
                    }
                });
                mPlayer1.prepareAsync();
            } catch (Exception e) {
                e.printStackTrace();
            }

            surfaceview = new SurfaceView(mContext);
            surfaceview.setZOrderOnTop(true);
            surfaceview.getHolder().setFormat(PixelFormat.TRANSLUCENT);
            surfaceview.getHolder().addCallback(new SurfaceHolder.Callback() {
                @Override
                public void surfaceCreated(SurfaceHolder holder) {
                    mPlayer1.setDisplay(holder);
//                    mPlayer1.prepareAsync();
                    Log.e(TAG, "surfaceCreated() returned: " + "创建");
                }

                @Override
                public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                    Log.e(TAG, "surfaceChanged() returned: " + "改变");
                }

                @Override
                public void surfaceDestroyed(SurfaceHolder holder) {
                    Log.e(TAG, "surfaceDestroyed() returned: " + "销毁");
                    mPlayer1.release();
                }
            });

            container.addView(surfaceview);
            return surfaceview;

        }*/
    }

    //PagerAdapter只缓存三张要显示的图片，如果滑动的图片超出了缓存的范围，就会调用这个方法，将图片销毁
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    //获取要滑动的控件的数量，
    public int getCount() {
        return listBean.size();
    }

    //来判断显示的是否是同一张照片，这个我们将两个图片对比 再返回
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }
}
